package com.meritit.config.test;

import java.io.IOException;
import java.io.InputStream;

public class ClassLoaderDemo {

	public static void main(String[] args) throws Exception {
		Class c1 = Class.forName("com.meritit.config.test.ClassLoaderDemo");
		System.out.println(c1.getClassLoader());
		
		ClassLoader mycl = new ClassLoader() {
			@Override
			public Class<?> loadClass(String name) throws ClassNotFoundException {
				String fileName = name.substring(name.lastIndexOf(".") + 1) + ".class";
				InputStream is = getClass().getResourceAsStream(fileName);
				if(is == null) {
					return super.loadClass(name);
				}
				
				try {
					byte[] buff = new byte[is.available()];
					is.read(buff);
					return defineClass(name, buff, 0, buff.length);
				} catch (IOException e) {
					e.printStackTrace();
				}
				
				return super.loadClass(name);
			}
		};
		
		Class clazz = mycl.loadClass("com.meritit.config.test.ClassLoaderDemo");
		System.out.println(clazz.getClassLoader());
		
		ClassLoaderDemo cld = new ClassLoaderDemo();
		System.out.println(cld.getClass().getClassLoader());
	}
	
}
